package com.neuedu.elmserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.elmserver.mapper.FoodMapper;
import com.neuedu.elmserver.pojo.Cart;
import com.neuedu.elmserver.mapper.CartMapper;
import com.neuedu.elmserver.pojo.Food;
import com.neuedu.elmserver.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.elmserver.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author wyh
 * @since 2024-11-11
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private FoodMapper foodMapper;

    /**
     * 添加购物车
     * @param cart
     * @return
     */
    @Override
    public ResultVO addCart(Cart cart) {
        cartMapper.insert(cart);
        return ResultVO.ok("添加成功");
    }

    /**
     * 查询某个用户在指定商家中的购物车列表
     * @param businessId
     * @param userId
     * @return
     */
    @Override
    public ResultVO listCart(Integer businessId, Integer userId) {
        QueryWrapper qw = new QueryWrapper(); //查询的条件构造器
        qw.eq("user_id", userId);
        qw.eq("business_id", businessId);
        return ResultVO.ok("查询成功", cartMapper.selectList(qw));
    }

    /**
     * 更新购物车
     * @param cart
     * @return
     */
    @Override
    public ResultVO updateCart(Cart cart) {
        //update set quantity=xx where business_id=xx and user_id=xx and food_id
        UpdateWrapper uw=new UpdateWrapper();//增删改的条件构造器
        uw.set("quantity",cart.getQuantity());
        uw.eq("business_id",cart.getBusinessId());
        uw.eq("user_id",cart.getUserId());
        uw.eq("food_id",cart.getFoodId());
        cartMapper.update(null,uw);
        return ResultVO.ok("更新成功");
    }

    /**
     * 删除购物车
     * @param cart
     * @return
     */
    @Override
    public ResultVO deleteCart(Cart cart) {
        //delete from cart where user_id=xx and business_id=xx and food_id=xx
        UpdateWrapper uw=new UpdateWrapper();//增删改的条件构造器
        uw.eq("business_id",cart.getBusinessId());
        uw.eq("user_id",cart.getUserId());
        uw.eq("food_id",cart.getFoodId());
        cartMapper.delete(uw);
        return ResultVO.ok("删除成功");
    }

    /**
     * 查询指定用户在所有商家中的购物车数量
     * @param userId
     * @return
     */
    @Override
    public ResultVO cartBusinessCount(Integer userId) {
        QueryWrapper<Cart> qw=new QueryWrapper();
        //SELECT sum(quantity) as carcount, business_id  as businessId FROM `cart` where user_id=6  group by business_id ;\
        //select():指定查询的列，一个qw只能有一个select方法
        qw.select("sum(quantity) as carcount ,business_id as businessId");
        qw.eq("user_id",userId);
        qw.groupBy("business_id");
        List<Map<String,Object>> mapList= cartMapper.selectMaps(qw);
        return ResultVO.ok("查询成功",mapList);
    }

    /**
     * 查询某个用户在指定商家中的购物车详情列表
     * @param businessId
     * @param userId
     * @return
     */
    @Override
    public ResultVO listCartDetails(Integer businessId, Integer userId) {
        //①先查询查询某个用户在指定商家中的购物车数据
        QueryWrapper qw=new QueryWrapper();//查询的条件构造器
        //select *from cart where business_id=xx and user_id=xx
        qw.eq("user_id",userId);
        qw.eq("business_id",businessId);
        List<Cart> listCart=cartMapper.selectList(qw);
        //②获取到每个购物项的foodId,在根据foodId查询每个购物项的食品数据
        for(Cart cart:listCart){
            Food food= foodMapper.selectById(cart.getFoodId());
            cart.setFood(food);
        }
        return ResultVO.ok("查询成功",listCart);
    }

    /**
     * 根据指定商家查询登录用户的购物车食品数量
     * @param businessId
     * @param userId
     * @return
     */
    @Override
    public ResultVO getBusinessQuantity(Integer businessId, Integer userId) {
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.select("SUM(quantity) AS businessCount")
                .eq("user_id", userId)
                .eq("business_id", businessId)
                .groupBy("business_id");

        Map<String, Object> result = cartMapper.selectMaps(qw).stream().findFirst().orElse(null);

        //System.out.println(result);
        return ResultVO.ok("查询成功", result);
    }
}





